package com.reyco1.data	
{
	import com.reyco1.events.SmartEventDispatcher;
	
	import flash.net.SharedObject;
	import flash.net.SharedObjectFlushStatus;
	
	public class Cookie extends SmartEventDispatcher
	{
		private var sharedObject:SharedObject;
		private var cookieName:String;
		
		public function Cookie(cookieID:String)
		{
			super();
			cookieName = cookieID;
			sharedObject = SharedObject.getLocal(cookieName);
		}
		
		public function clear():void
		{
			sharedObject.clear();
		}
		
		public function setValue(id:String, value:*):Boolean
		{
			sharedObject.data[id] = value;
			try 
			{
				return processResult(sharedObject.flush());
			} 
			catch(error:Error)
			{
				log("Cannot save!", "warning");
			}
			return false;
		}
		
		public function getValue(id:String):*
		{
			return sharedObject.data[id];
		}
		
		private function processResult(result:String):Boolean
		{
			var bolStatus:Boolean;
			
			switch(result)
			{
				case SharedObjectFlushStatus.FLUSHED:
					bolStatus = true;
					break;
				
				case SharedObjectFlushStatus.PENDING:
					bolStatus = false;
					break;
			}
			
			return bolStatus;
		}
		
	}
}